package com.aegean.MonitorExamSYS.center.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.aegean.MonitorExamSYS.center.business.CenterBusiness;
import com.opensymphony.xwork2.ActionSupport;

/*ConfirmArrageForCP is the acronym of
 ConfirmArragementForConductionAndPosition
 确定考点和考场的安排Action
 */
@Controller("ConfirmArrageForCP")
@Scope("prototype")
public class ConfirmArrageForCP extends ActionSupport implements
		ServletRequestAware {
	@Resource
	CenterBusiness business;
	private HttpServletRequest request;
	private HttpSession session;
	private Map<Integer, List<Integer>> con_PositionsMap;
	private Map<Integer, Integer> con_PositionCountMap;
	private boolean success;
	private String msg;
	private int exam_Id;
	private Date exam_StartTime;

	public int getExam_Id() {
		return exam_Id;
	}

	public void setExam_Id(int exam_Id) {
		this.exam_Id = exam_Id;
	}

	public Date getExam_StartTime() {
		return exam_StartTime;
	}

	public void setExam_StartTime(Date exam_StartTime) {
		this.exam_StartTime = exam_StartTime;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		con_PositionsMap = (Map<Integer, List<Integer>>) session
				.getAttribute("confirmConductionsMap");
		con_PositionCountMap = (Map<Integer, Integer>) session
				.getAttribute("confirmPositionPersonCount");

		success = business.confirmArrageForCP(con_PositionsMap,
				con_PositionCountMap, exam_Id, exam_StartTime);
		if (success == false) {
			//System.out.println("出错");
			msg="对不起，总设定出现错误，请联系管理员！";
		} else {
			//System.out.println("正確");
			msg="考场和考点总设定安排成功！";
			recycle();
		}
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
		this.session = request.getSession();

	}

	public void recycle() {
		String[] deleteSession = { "MonitorSelectedMap",// 删除对应考场分配的监考人员信息
				"confirmPositionPersonCount",// 删除考点考生数量的分配
				"autoArrangeConductionPersonCount", "ConListForCenter" };// 删除自动安排考点相应的人数分配
		// 删除点击考场信息考点列表信息
		for (String string : deleteSession) {
			if (session.getAttribute(string) == null) {
				continue;
			} else {
				session.removeAttribute(string);
			}
		}
	}

}
